#include <iostream>
#include <signal.h>
void printPending(sigset_t sig)
{
    std::cout << "the corrent process [" << getpid() <<"]'s penging is: ";
    for (int i = 31; i >= 1; i--)
    {
        if (sigismember(&sig, i))
            std::cout << 1;
        else
            std::cout << 0;
    }
    std::cout << std::endl;
}
void handle(int sig)
{
    std::cout << "信号 << sig << 递达啦！" << std::endl;
    sigset_t pending;
    sigpending(&pending);

    //可以验证在pending信号集中，信号在递达之前就已经设置为0了：
    std::cout << "------------------------------------------" << std::endl;
    printPending(pending);
    std::cout << "------------------------------------------" << std::endl;
}
int main()
{
    sigset_t set, oldset;
    sigemptyset(&set);
    sigemptyset(&oldset);
    sigaddset(&set, SIGINT);

    //0.完成对2号信号的捕获：
    signal(SIGINT, handle);

    // 1.完成了对2号信号的屏蔽操作：
    sigprocmask(SIG_BLOCK, &set, &oldset);

    int count = 10;
    while (true)
    {
        // 2.获取当前进程的pending信号集：
        sigset_t pending;
        sigpending(&pending);

        //3.打印pending信号集：
        printPending(pending);

        count--;
        sleep(1);
        //4.在经过10s后解除对2号信号的屏蔽：
        if(count == 0)
        {
            std::cout << "relieve blocking on 2..." << std::endl;
            sigprocmask(SIG_UNBLOCK, &set, &oldset);
        }
    }
    return 0;
}